<?php

namespace App\Http\Middleware;

use App\Model\System\Token;
use Closure;
use App\Model\System\Syslog;
use Illuminate\Support\Facades\DB;

class AfterMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        # 记录每个请求的系统日志
        $this->syslog($request, $response);

        # 打印调试sql
      //  $this->dumpsql();

        return $response;
    }


    /**
     * 记录系统日志
     * @param $request
     * @param $response
     */
    protected function syslog($request, $response)
    {
        $now = date('Y-m-d H:i:s');
        $loger = new Syslog();
        $loger->logtime = $now;
        $loger->createdate = $now;
        $loger->url = $request->fullUrl();
        $loger->inputparms = json_encode($request->all());
        $loger->returnvalue = $response->getContent();
        if(Token::user()){
            $loger->createby = Token::user()->userid;
        }
        $loger->save();
    }

    function dumpsql()
    {
        dd(DB::getQueryLog());
    }
}
